<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">



  <meta name="description" content="Django自带认证系统邮件模板自定义"/>




  <meta name="keywords" content="开发, Django, 八一" />



  <meta name="baidu-site-verification" content="HhUstaSjr0" />



  <meta name="google-site-verification" content="UA-102975942-1" />






  <link rel="alternate" href="/atom.xml" title="八一">




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=2.6.0" />



<link rel="canonical" href="https://bay1.top/2018/02/01/Django自带认证系统邮件模板自定义/"/>


<link rel="stylesheet" type="text/css" href="/css/style.css?v=2.6.0" />
<link rel="stylesheet" type="text/css" href="/css/prettify.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/css/sons-of-obsidian.css" media="screen" />



  <link rel="stylesheet" type="text/css" href="/lib/fancybox/jquery.fancybox.css" />




  
  <script id="baidu_analytics">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?9a885cc9fb6cd7bcef579deb8efe8a70";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>



  <script id="google_analytics">
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
        })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

        ga('create', 'UA-102975942-1', 'auto');
        ga('send', 'pageview');
  </script>










    <title> Django自带认证系统邮件模板自定义 - 八一 </title>
  </head>

  <body><div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/." class="logo">八一</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>

<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    
      <a href="/archives">
        <li class="mobile-menu-item">
          
          
            文章
          
        </li>
      </a>
    
      <a href="/tags">
        <li class="mobile-menu-item">
          
          
            标签
          
        </li>
      </a>
    
      <a href="/about">
        <li class="mobile-menu-item">
          
          
            关于/友链
          
        </li>
      </a>
    
      <a href="/search">
        <li class="mobile-menu-item">
          
          
            站内搜索
          
        </li>
      </a>
    
  </ul>
</nav>

    <div class="container" id="mobile-panel">
      <header id="header" class="header"><div class="logo-wrapper">
  <a href="/." class="logo">八一</a>
</div>

<nav class="site-navbar">
  
    <ul id="menu" class="menu">
      
        <li class="menu-item">
          <a class="menu-item-link" href="/archives">
            
            
              文章
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/tags">
            
            
              标签
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/about">
            
            
              关于/友链
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/search">
            
            
              站内搜索
            
          </a>
        </li>
      
    </ul>
  
</nav>

      </header>

      <main id="main" class="main">
        <div class="content-wrapper">
          <div id="content" class="content">
            
  
  <article class="post">
    <header class="post-header">
      <h1 class="post-title">
        
          Django自带认证系统邮件模板自定义
        
      </h1>

      <div class="post-meta">
        <span class="post-time">
          2018-02-01
        </span>
        
        
        
      </div>
    </header>

    
    
  <div class="post-toc" id="post-toc">
    <h2 class="post-toc-title">文章目录</h2>
    <div class="post-toc-content">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#怎么办？？"><span class="toc-text">怎么办？？</span></a></li></ol>
    </div>
  </div>


    <div class="post-content">
      
        <p>这几天部署Docker设置Nginx遇到了邮件发送问题<a id="more"></a><br>首先我的问题就是,在服务器上部署Docker之后<br>然后宿主机的Nginx设置代理</p>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">server</span><br><span class="line">&#123;</span><br><span class="line">      <span class="attribute">listen</span> <span class="number">80</span>;</span><br><span class="line">      <span class="attribute">server_name</span> cumt.studio www.cumt.studio;</span><br><span class="line">      <span class="attribute">location</span> / &#123;</span><br><span class="line">        <span class="attribute">proxy_redirect</span> <span class="literal">off</span>;</span><br><span class="line"></span><br><span class="line">        <span class="attribute">proxy_set_header</span> Host <span class="variable">$host</span>;</span><br><span class="line">        <span class="attribute">proxy_set_header</span> X-Real-IP <span class="variable">$remote_addr</span>;</span><br><span class="line">        <span class="attribute">proxy_set_header</span> X-Forwarded-For <span class="variable">$proxy_add_x_forwarded_for</span>;</span><br><span class="line">        <span class="attribute">proxy_pass</span> http://127.0.0.1:8003;</span><br><span class="line">      &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>proxy_pass监听了Docker内网端口<br>然后代码使用了Django自带的认证系统<br>以及封装好的Django-registration-redux注册模块<br>使用上面的Nginx接收的邮件里只有example.com</p>
</blockquote>
<p><img src="https://s1.ax1x.com/2018/02/02/9AlQl4.png" alt="error"></p>
<p>然后找到了一个解决办法<br>需要设置proxy_host,将host代理为你的域名</p>
<blockquote>
<p>好吧…..我太菜<br>不是很懂Nginx配置,搜了好久,搞了好久<br>也问了一些人,我放弃了………..<br>(或许以后某一天就懂了….orzzz)</p>
</blockquote>
<h2 id="怎么办？？"><a href="#怎么办？？" class="headerlink" title="怎么办？？"></a>怎么办？？</h2><blockquote>
<p>然后想到flask中模板可以自定义<br>讲道理这个Django应该也是可以的吧<br>然后又是一顿乱搜….<br>最后还是回归到了官方文档</p>
</blockquote>
<p>关于Django自带的认证系统<br>文档(看的翻译版本)这样写的<br>以重置密码为例</p>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">password_reset(</span><br><span class="line">	request,</span><br><span class="line">	<span class="attribute">template_name</span>=<span class="string">'registration/password_reset_form.html'</span>,</span><br><span class="line">	<span class="attribute">email_template_name</span>=<span class="string">'registration/password_reset_email.html'</span>,</span><br><span class="line">	<span class="attribute">subject_template_name</span>=<span class="string">'registration/password_reset_subject.txt'</span>,</span><br><span class="line">	<span class="attribute">password_reset_form</span>=PasswordResetForm,</span><br><span class="line">	<span class="attribute">token_generator</span>=default_token_generator,</span><br><span class="line">	<span class="attribute">post_reset_redirect</span>=None,</span><br><span class="line">	<span class="attribute">from_email</span>=None,</span><br><span class="line">	<span class="attribute">current_app</span>=None,</span><br><span class="line">	<span class="attribute">extra_context</span>=None,</span><br><span class="line">	<span class="attribute">html_email_template_name</span>=None,</span><br><span class="line">	<span class="attribute">extra_email_context</span>=None</span><br><span class="line">)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>允许用户通过生成一次性使用链接来重置密码<br>并将该链接发送到用户的注册电子邮件地址</p>
</blockquote>
<p>如果系统中不存在提供的电子邮件地址<br>此视图将不会发送电子邮件,但用户也不会收到任何错误消息<br>这防止信息泄露给潜在的攻击者,如果在这种情况下要提供错误消息<br>可以将 PasswordResetForm 子类化并使用 password_reset_form 参数</p>
<p>用户被标记为不可用的密码(请参阅 set_unusable_password() 不允许请求重置密码以防止在使用LDAP等外部认证源时发生误操作)<br>注意,他们不会收到任何错误消息,因为这将暴露其帐户的存在,但没有邮件也将被发送</p>
<p>可选参数：</p>
<ul>
<li><p>template_name：用于显示密码重置表单的模板的全名如果未提供,则默认为 registration/password_reset_form.html</p>
</li>
<li><p>email_template_name：用于使用重置密码链接生成电子邮件的模板的全名如果未提供,则默认为 registration/password_reset_email.html</p>
</li>
<li><p>subject_template_name：用于具有重置密码链接的电子邮件主题的模板的全名如果未提供,则默认为 registration/password_reset_subject.txt</p>
</li>
<li><p>password_reset_form：用于获取用户重置密码的电子邮件的表单默认为 PasswordResetForm</p>
</li>
<li><p>token_generator：类的实例检查一次性链接这将默认为 default_token_generator,它是 django.contrib.auth.tokens.PasswordResetTokenGenerator 的实例</p>
</li>
<li><p>post_reset_redirect：成功重置密码后重定向到的网址</p>
</li>
<li><p>from_email：有效的电子邮件地址默认情况下,Django使用 DEFAULT_FROM_EMAIL</p>
</li>
<li><p>html_email_template_name：用于生成带有密码重置链接的 text/html 分段电子邮件的模板的全名默认情况下,不发送HTML电子邮件</p>
</li>
<li><p>extra_email_context：电子邮件模板中将提供的上下文数据字典</p>
</li>
</ul>
<p>模板上下文：</p>
<ul>
<li>form：用于重置用户密码的表单(请参阅上面的 password_reset_form)</li>
</ul>
<p>电子邮件模板上下文：</p>
<ul>
<li><p>email：user.email 的别名</p>
</li>
<li><p>user：当前 User,根据 email 表单字段只有活动用户才能重置其密码(User.is_active is True)</p>
</li>
<li><p>site_name：site.name 的别名如果你没有安装网站框架,这将被设置为 request.META[‘SERVER_NAME’] 的值有关网站的更多信息,请参见 “网站”框架</p>
</li>
<li><p>domain：site.domain 的别名如果你没有安装网站框架,这将被设置为 request.get_host() 的值</p>
</li>
<li><p>protocol：http或https</p>
</li>
<li><p>uid：用base64编码的用户主键</p>
</li>
<li><p>token：用于检查复位链接是否有效的令牌</p>
</li>
</ul>
<p>示例 registration/password_reset_email.html(电子邮件正文模板)：</p>
<figure class="highlight xquery"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Someone asked <span class="keyword">for</span> password reset <span class="keyword">for</span> email &#123;&#123; email &#125;&#125;. Follow the link below:</span><br><span class="line">&#123;&#123; protocol&#125;&#125;://&#123;&#123; domain &#125;&#125;&#123;% url <span class="string">'password_reset_confirm'</span> uidb64=uid token=token %&#125;</span><br></pre></td></tr></table></figure>
<p>很明显了,直接添加下面的文件到registration/password_reset_email.html</p>
<figure class="highlight xquery"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">你收到这封邮件是因为你请求重置平台的账户密码</span><br><span class="line"></span><br><span class="line">如果这个用户名是属于您的话： &#123;&#123; email &#125;&#125;</span><br><span class="line"></span><br><span class="line">请访问该页面并选择一个新密码：</span><br><span class="line"></span><br><span class="line">&#123;&#123; protocol&#125;&#125;://自己的域名&#123;% url <span class="string">'password_reset_confirm'</span> uidb64=uid token=token %&#125;</span><br><span class="line"></span><br><span class="line">&#123;&#123; request.META &#125;&#125;</span><br><span class="line"></span><br><span class="line">否则,请忽略这封邮件！</span><br><span class="line"></span><br><span class="line">感谢使用我们的站点！</span><br><span class="line"></span><br><span class="line">翔工作室技术组</span><br></pre></td></tr></table></figure>
<blockquote>
<blockquote>
<p>现在就是说site_name和domain是默认链接里的网址<br>我把域名写死,就发不错了…………..<br>(这里可以获取 request.META.HTTP_HOST 获取网址,但是重置密码的邮件还是获取不到,我就还是写死了./.)</p>
</blockquote>
</blockquote>
<p><img src="https://s1.ax1x.com/2018/02/02/9A3mzF.jpg" alt="orz"></p>
<p>同理,在注册页面的django-registration-redux官方文档处<br>没找到……….默认模板的命名方式<br>然后,我就在万能的github搜了一下,activation_email.html(忘了怎么想到是搜这个词的了)<br>就是这个命名,仿照上面的格式在registration新建就行了</p>
<blockquote>
<p>如果要更改邮件标题<br>可以同一目录添加activation_email_subject.txt和password_reset_subject.txt</p>
</blockquote>
<p>虽然是个很笨的方法,但是几天的问题解决了</p>
<p><hr></p>
<center>2018/4/18更新</center>

<blockquote>
<p>这个项目在Djanago自带后台有站点设置,修改之后可以正常显示邮件中信息<br>不过如果想要自定义邮件模板还是要如上文一样新建html</p>
</blockquote>
<p>链接:<br><a href="https://www.rddoc.com/doc/Django/1.10.5/zh/topics/auth/default/#module-django.contrib.auth.views" target="_blank" rel="noopener">Django认证系统视图</a><br><a href="http://django-registration-redux.readthedocs.io/en/latest/quickstart.html" target="_blank" rel="noopener">Django-registration-redux文档</a></p>

      
    </div>

    
      
      



      
      
    

    
      <footer class="post-footer">
        
          <div class="post-tags">
            
              <a href="/tags/开发/">开发</a>
            
              <a href="/tags/Django/">Django</a>
            
          </div>
        
        
        
  <nav class="post-nav">
    
      <a class="prev" href="/2018/02/07/axios如何发送Basic-Auth/">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">axios如何发送Basic Auth</span>
        <span class="prev-text nav-mobile">上一篇</span>
      </a>
    
    
      <a class="next" href="/2018/01/31/Ajax与Flask传值的跨域问题/">
        <span class="next-text nav-default">Ajax与Flask传值的跨域问题</span>
        <span class="prev-text nav-mobile">下一篇</span>
        <i class="iconfont icon-right"></i>
      </a>
    
  </nav>

      </footer>
    

  </article>


          </div>
          
  <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div> 
    </div>
  </div>


        </div>
      </main>

      <footer id="footer" class="footer">

  <div class="social-links">
    
      
        
          <a href="https://github.com/bay1" class="iconfont icon-github" title="github"></a>
        
      
    
      
        
          <a href="http://weibo.com/3190704711/profile?topnav=1&wvr=6&is_all=1" class="iconfont icon-weibo" title="weibo"></a>
        
      
    
      
    
      
    
      
    
    
    
  </div>


<div class="copyright">
  <span class="copyright-year">
    
    &copy; 
     
      2016 - 
    
    2018
    <span class="author">bay1</span>
  </span>
</div>
      </footer>

      <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
      </div>
    </div>

    
  
  <script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'https://bay1.top/2018/02/01/Django自带认证系统邮件模板自定义/';
        this.page.identifier = '2018/02/01/Django自带认证系统邮件模板自定义/';
        this.page.title = 'Django自带认证系统邮件模板自定义';
    };
    (function() {
    var d = document, s = d.createElement('script');

    s.src = '//https-blog-flywinky-top-1.disqus.com/embed.js';

    s.setAttribute('data-timestamp', +new Date());
    (d.head || d.body).appendChild(s);
    })();  
  </script>



    
  





  
    <script type="text/javascript" src="/lib/jquery/jquery-3.1.1.min.js"></script>
  

  
    <script type="text/javascript" src="/lib/slideout/slideout.js"></script>
  

  
    <script type="text/javascript" src="/lib/fancybox/jquery.fancybox.pack.js"></script>
  


    <script type="text/javascript" src="/js/src/even.js?v=2.6.0"></script>
<script type="text/javascript" src="/js/src/bootstrap.js?v=2.6.0"></script>
<script src="/js/prettify.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 $('pre').addClass('prettyprint');
   prettyPrint();
 })
</script>
  </body>
</html>
